/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.caching.config;

import io.iec.edp.caf.caching.aspect.LayeringAspect;
import io.iec.edp.caf.caching.properties.JedisPoolConfigProperties;
import io.iec.edp.caf.caching.properties.RedisCacheProperties;
import io.iec.edp.caf.caching.api.CacheManager;
import io.iec.edp.caf.caching.manager.LayeringCacheManager;
import io.iec.edp.caf.caching.properties.LayeringCacheProperties;
import io.iec.edp.caf.commons.caching.CacheFactory;
import io.iec.edp.caf.commons.caching.JedisPoolManager;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;

/**
 * 多级缓存自动配置类
 */
@Configuration(proxyBeanMethods = false)
//@ConditionalOnBean(RedisTemplate.class)
//@AutoConfigureAfter({RedisAutoConfiguration.class})
@EnableAspectJAutoProxy
@EnableConfigurationProperties({
        JedisPoolConfigProperties.class,
        LayeringCacheProperties.class,
        RedisCacheProperties.class,
        io.iec.edp.caf.commons.layeringcache.properties.JedisPoolConfigProperties.class,
        io.iec.edp.caf.commons.layeringcache.properties.LayeringCacheProperties.class,
        io.iec.edp.caf.commons.layeringcache.properties.RedisCacheProperties.class,
})
public class LayeringCacheAutoConfiguration {

    /**
     * @param properties
     * @return
     */
    @Bean
    public CacheManager cafLayeringCacheManager(RedisCacheProperties redisCacheSettings, JedisPoolConfigProperties jedisPoolConfigProperties, LayeringCacheProperties properties) {
        LayeringCacheManager layeringCacheManager = new LayeringCacheManager(redisCacheSettings, jedisPoolConfigProperties);
        // 默认开启统计功能
        layeringCacheManager.setStats(properties.isStats());
        return layeringCacheManager;
    }

    /**
     * @param properties
     * @return
     */
    @Bean
    public io.iec.edp.caf.commons.layeringcache.manager.CacheManager cafOldLayeringCacheManager(io.iec.edp.caf.commons.layeringcache.properties.RedisCacheProperties redisCacheSettings, io.iec.edp.caf.commons.layeringcache.properties.JedisPoolConfigProperties jedisPoolConfigProperties,io.iec.edp.caf.commons.layeringcache.properties.LayeringCacheProperties properties) {
        io.iec.edp.caf.commons.layeringcache.manager.LayeringCacheManager layeringCacheManager = new io.iec.edp.caf.commons.layeringcache.manager.LayeringCacheManager(redisCacheSettings, jedisPoolConfigProperties);
        // 默认开启统计功能
        layeringCacheManager.setStats(properties.isStats());
        return layeringCacheManager;
    }

    @Bean
    public LayeringAspect cafLayeringAspect() {
        return new LayeringAspect();
    }

    //声明单例还是原型
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    //是否为懒加载。默认为懒加载就是容器启动后就直接创建实例
    @Lazy(value = false)
    //声明bean的信息等同于xml的方式
    @Bean
    public CacheFactory cacheFactory() {
        return new CacheFactory();
    }

    //声明单例还是原型
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    //是否为懒加载。默认为懒加载就是容器启动后就直接创建实例
    @Lazy(value = false)
    //声明bean的信息等同于xml的方式
    @Bean
    public JedisPoolManager jedisPoolManager() {
        return new JedisPoolManager();
    }
}
